TopicWindow = function(){
	var data = [];
	var column = [
		new Ext.grid.RowNumberer(),
		{
			dataIndex:'title',
			header:'已选栏目',
			width:120
		}
	];
	 var formatDate = function(value){
	 	/*var date = new Date(value);
	 	var str = Ext.util.Format.date(date,'Y-m-d');
        return str;*/
	 	return value;
    }
    //删除一条记录信息
	var deleteOnlyOneInfo = function(grid, rowIndex, colIndex) {
        var rec = keywordstore.getAt(rowIndex);
        Ext.Msg.confirm('系统提示','是否确定删除选择的数据',function(btn){
			if(btn=='yes'){
				Ext.Ajax.request(
					{
						url:__ctxPath+'/back/article/deletetitleinfo.html',
						method:'POST',
						params:{ids:rec.get('tid')},
						success:function(resp,option){
							var result = Ext.decode(resp.responseText);
							if(result.success){
								keywordstore.remove(rec);
								center.getView().refresh();
								SX.Util.msg('系统提示','删除成功!');
							}else{
								msg('系统提示','删除失败',Ext.MessageBox.WARNING);
							}
						},
						failure:function(resp,option){
							Ext.MessageBox.alert('系统提示','网络原因,删除失败');
						}
					}
				);
			}
		});
    }
	var dictwords = [
		new Ext.grid.RowNumberer(),
		{
			dataIndex:'title',
			header:'栏目名称',
			width:170
		},{
			dataIndex:'tdatetime',
			renderer: formatDate,
			header:'编辑时间',
			width:130
		},{
            xtype: 'actioncolumn',
            header:'操作',
            width: 50,
            items: [ {
                iconCls: 'tab_delete-col',
                tooltip: '删除栏目',
                handler: deleteOnlyOneInfo
            }]
        }
	];	
	var store = new Ext.data.JsonStore(
		{
			data:data,
			autoLoad:true,
			fields:['tid','title','tdatetime']
		}
	);
	var keywordstore = new Ext.data.JsonStore(
		{
			url : __ctxPath+'/back/article/findtitleinfo.html',
			baseParams:{start:0,limit:10,userid:userinfo.uid},
			totalProperty: 'totalCount', 
			root: 'data',
			autoLoad:true,
			/*data:[
				{tid:1,title:'你傻',tdatetime:'2011-01-01'},
				{tid:2,title:'你二',tdatetime:'2012-01-01'},
				{tid:3,title:'你呆',tdatetime:'2012-03-03'}
			],*/
			fields:['tid','title','tdatetime']
		}
	); 
	var searchKeyWords = function(name){
		var _obj = {start:0,limit:10,title:name};
		var _url = __ctxPath+'/back/keyword/findkeywordsbyuserid.html';
		var _store=keywordstore;
		_store.proxy.setUrl(_url,true);
		_store.baseParams = _obj;
		_store.load({params:_obj});
	}
	var search = function(){
		var form = north.getForm();
		var txtname = form.findField('searchname').getValue();
		searchKeyWords(txtname);
	};
	var histroyinfo =  function(){
		var _obj = {start:0,limit:10,userid:userinfo.uid};
		var _url = __ctxPath+'/back/keyword/findkeywordshistroybyuserid.html';
		var _store=keywordstore;
		_store.proxy.setUrl(_url,true);
		_store.baseParams = _obj;
		_store.load({params:_obj});
	};
	
	var addTitle = function(){
		Ext.MessageBox.prompt('系统提示', '请填写所需要的栏目信息,不能超过10个字！', 
			function(btn,text){
				if(btn=='ok'){
					if(text.length<=10&&text.length>0){
						Ext.Ajax.request(
							{
								url:__ctxPath+'/back/article/addtitleinfo.html',
								params:{title:text,tuserid:userinfo.uid},
								method:'POST',
								success:function(resp,option){
									var obj = Ext.decode(resp.responseText);
									if(obj.success){
										SX.Util.msg('系统提示','栏目信息添加成功');
										keywordstore.reload();
									}else if(obj.success==false&&obj.ishaveit==true){
										SX.Util.msg('系统提示','该栏目信息已经存在');
										searchKeyWords(text);
									}else{
										msg('系统提示','栏目信息添加失败',Ext.MessageBox.WARNING);
									}
									
								},
								failure:function(resp,option){
									msg('系统提示','栏目信息添加失败',Ext.MessageBox.WARNING);
								}
							}
						);	
					}else{
						msg('系统提示','栏目信息在1~10个字之间',Ext.MessageBox.WARNING);
						return;
					}
				}
			}
		);
	};
	var north = new Ext.FormPanel(
		{
			height:38,
			region:"north",
			layout:"hbox",
			bodyStyle:"padding:6px 2px 2px 2px",
			layoutConfigs:{
				align:"middle"
			},
			keys:{
				key:Ext.EventObject.ENTER,
				scope:this,
				fn:search
			},
			defaultType:"label",
			defaults:{margins:"0 0 0 4"},
			items:[
				{xtype:"textfield",name:"searchname",width:260,maxLength:256},
				{xtype:"button",text:'查找',iconCls:"zoom",scope:this,handler:search},
				{xtype:"button",text:'历史记录',iconCls:"calendar_view_day",scope:this,handler:histroyinfo},
				{xtype:"button",text:'添加栏目',iconCls:"tab_add",scope:this,handler:addTitle}
			]
		}
	);
	var selectedUserGrid = new Ext.grid.GridPanel(
		{
			id:'selectedUserGrid',
			title:'',
			store:store,
//			autoExpandColumn : 'userid',
			columns: column,
			height:400,
			width:200,
			region:'center'
		}
	);
	selectedUserGrid.on('rowdblclick',function(grid,index,e){
		var c=grid;
		var d=c.getSelectionModel().getSelections();
		var a=c.getStore();
		for(var b=0;b<d.length;b++) {
			c.stopEditing();
			a.remove(d[b]);
		}
	});
	var addAll = function () {
		var g=Ext.getCmp("contactGrid");
		var a=Ext.getCmp("selectedUserGrid");
		var h=a.getStore();
		var m=g.getSelectionModel().getSelections();
		for(var c=0;c<m.length;c++) {
			var d=m[c].data.tid;
			var k=m[c].data.title;
			var f=false;
			for(var b=0;b<h.getCount();b++) {
				if(h.getAt(b).data.tid==d) {
					f=true;
					break;
				}
			}
			if(!f) {
				var l={
					tid:d,
					title:k
				};
				var e=new h.recordType(l);
				a.stopEditing();
				h.add(e);
			}
		}
	};
	
	var removeAll=function () {
		var c=Ext.getCmp("selectedUserGrid");
		var d=c.getSelectionModel().getSelections();
		var a=c.getStore();
		for(var b=0;b<d.length;b++) {
			c.stopEditing();
			a.remove(d[b]);
		}
	};
	
	var tobarinfo = new Ext.Panel(
		{
			region:"west",
			frame:true,
			width:40,
			layout:{type:"vbox",pack:"center",align:"stretch"},
			defaultType:"button",
			items:[
				{
//					iconCls:"rightarrow",
					iconCls:"btn-right",
					tooltip:"添加",
					scope:this,
					handler:addAll
				},{
//					iconCls:"leftarrow",
					iconCls:"btn-left",
					tooltip:"去掉",
					scope:this,
					handler:removeAll
				}
			]
		}
	);
	var east=new Ext.Panel({
		layout:"border",
		region:"east",
		width:220,
		height:"100%",
		border:false,
		autoScroll:true,
		items:[
			tobarinfo,
			selectedUserGrid
		]
	});
	var bbar = new Ext.PagingToolbar({
		store:keywordstore,
		pageSize:10,
		displayInfo: true,   
		displayMsg:'显示第{0}至{1}条,共{2}条'
     });
	var center = new Ext.grid.GridPanel(
		{
			region:'center',
			iconCls:'tab',
			title:'栏目列表',
			id:'contactGrid',
			store:keywordstore,
			columns: dictwords,
			height:400,
			width:380,
			bbar:bbar
		}
	)
	center.on("rowdblclick",function (grid,index,e) {
		var g=Ext.getCmp("contactGrid");
		var a=Ext.getCmp("selectedUserGrid");
		var h=a.getStore();
		var m=g.getSelectionModel().getSelections();
		for(var c=0;c<m.length;c++) {
			var d=m[c].data.tid;
			var k=m[c].data.title;
			var f=false;
			for(var b=0;b<h.getCount();b++) {
				if(h.getAt(b).data.tid==d) {
					f=true;
					break;
				}
			}
			if(!f) {
				var l={
					tid:d,
					title:k
				};
				var e=new h.recordType(l);
				a.stopEditing();
				h.add(e);
			}
		}
	});
	var confirm = function(){
		var topics = [];
		var p = [];
		for (var i = 0; i < store.getCount(); i++) {
			var cm = store.getAt(i);
			topics.push(cm.data);
			p.push(cm.get('tid'));
		}
		this.fireEvent('submit',this,topics,p);
	}
	var cancel = function(){
		this.close();
	}
	var buttons = [
		{
			text:'确定',
			iconCls:"confirm",
			handler:confirm,
			scope:this
		},
		{
			text:'取消',
			iconCls:"cancel",
			handler:cancel,
			scope:this
		}
	];
	var config = {
		layout:'border',
		constrainHeader:true,
		constrain:true,
		modal : true,
		title: '编辑栏目',
		iconCls:'tab_edit',
        width : 620,
		height : 500,
        minWidth: 300,
        minHeight: 200,
        plain:true,
        bodyStyle:'padding:5px;',
        buttonAlign:'center',
        buttons:buttons,
		items:[north,east,center]
	}
	TopicWindow.superclass.constructor.call(this,config);
}
Ext.extend(TopicWindow,Ext.Window);